# -*- coding: utf-8 -*-
#Under the terms of the General Public License v.3
#For more information see LICENSE.txt

import pygame
from pygame.locals import K_LEFT, K_RIGHT, K_UP, K_DOWN, QUIT, KEYDOWN

#from menu import *
#from sound import *


#from item import *
from element import *
from infoimages import *

WIDTH = 640
HEIGTH = 480

class Scenario(object):


	def __init__(self, elements = None):
		#This initiate the matrix of the map
		self.matrix = self.loadMatrix ('./data/maps/Map.txt')
		#self.blocks = self.loadBlockImage()
		self.backgrown = self.newBack()
		self.drawBack()
		self.camera_x = 0
		self.camera_y = 0
		self.destination_camera_x = 0
		self.destination_camera_y = 0
		if elements:
			self.elements = elements
		else:
			self.elements = self.loadElements()

	def loadMatrix(self, path):
		#this funtion return de content of Map.txt
		fileMap = file(path,'rt')
		content = fileMap.readlines()
		fileMap.close()
		return content
	"""
	def loadBlockImage(self):
		#this funtion return a list of all block image of de map
		blocks = []
		for n in range(3):
			blocks.append(pygame.image.load('./data/image/%d.png' %n).convert_alpha())
		return blocks
	"""
	def newBack(self):
		#this funtion return a surface size
		arrows = len(self.matrix)
		columns =  (len(self.matrix[0]) - 1)
		width = columns * 48
		heigth = arrows * 48
		return pygame.Surface( (width, heigth) ).convert()

	def drawBack(self):
		numberOfArrows = len(self.matrix)
		numberOfColumns = (len(self.matrix[0]) - 1)

		self.backgrown.fill((150, 150, 255))
		"""
		for arrow in range(numberOfArrows):
			for column in range(numberOfColumns):
				index = int(self.matrix[arrow][column])
				pos_x = column * 48
				pos_y = arrow * 48
				self.backgrown.blit(self.blocks[index], (pos_x, pos_y))
		"""
	def loadElements(self):
		elements =  []
		numberOfArrows = len(self.matrix)
		numberOfColumns = (len(self.matrix[0]) - 1)
		for arrow in range(numberOfArrows):
			for column in range(numberOfColumns):
				pos_x = column * 48
				pos_y = arrow * 48
				item = self.matrix[arrow][column]
				try:
					itemvalues=ELEMENT_DICT.get(item)
					newelement=itemvalues[0](itemvalues[1],pos_X,pos_y) #falta pasarle los parámetros extras
					elements.append(newelement)
				except:
					print "BIG MEGAFAILURE LOCASO getting %s element" %item
		return elements

	def show(self, screen):
		origin = (self.camera_x, self.camera_y, WIDTH, HEIGTH)
		destination = (0, 0, WIDTH, HEIGTH)
		screen.blit(self.backgrown, destination, origin)

	def move(self, dx, dy):
		self.destination_camera_x += dx
		self.destination_camera_y += dy
		self.limits()

	def update(self):
		self.camera_x += (self.destination_camera_x - self.camera_x) / 10.0
		self.camera_y += (self.destination_camera_y - self.camera_y) / 10.0

	def limits(self):
		if self.destination_camera_x < 0:
			self.destination_camera_x = 0

		if self.destination_camera_y < 0:
			self.destination_camera_y = 0

		if self.destination_camera_x + WIDTH > self.backgrown.get_width():
			self.destination_camera_x = self.backgrown.get_width() - WIDTH

		if self.destination_camera_y + HEIGTH > self.backgrown.get_height():
			self.destination_camera_y = self.backgrown.get_height() - HEIGTH

	def lookAt(self, rect):
		self.destination_camera_x = rect.x - WIDTH / 2
		self.destination_camera_y = rect.y - HEIGTH / 2
		self.limits()

class Engine(object):

	def getMap(self):
		pass

	def setMap(self):
		pass

class EngineObjects(object):

	def __init__(self):
		self.initConstants()

	def initConstants(self):
		self.NUM_AP = {}
		self.NUM_AP[0] = [3, 3, 2]
		self.NUM_AP[1] = [2, 2, 2]
		self.NUM_AP[2] = [2, 1, 2, 1]

		self.NUM_HUMAN = {}
		self.NUM_HUMAN[0] = [1, 1, 2]
		self.NUM_HUMAN[1] = [2, 2, 2]
		self.NUM_HUMAN[2] = [2, 2, 3, 4]

		self.NUM_EV_ITEMS = {}
		self.NUM_EV_ITEMS[0] = [2, 3, 3]
		self.NUM_EV_ITEMS[1] = [3, 3, 4]
		self.NUM_EV_ITEMS[2] = [3, 4, 5, 6]

	def setMap(self, screen):
		self.screen = screen

	def startLevel(self, level, scenario):
		i = 0
		while (i < self.NUM_AP[level][scenario]):
			ap = AccessPoint(InfoImages.getImage(InfoImages.AP), i*100, i*100, 200)
			ap.paint(screen)
			i += 1


